#!/bin/sh -ex
port=$1
[ -z "$port" ] && port=5300
limit=$2
[ -z "$limit" ] && limit=100000

: ${RECURSOR:="../pdns/pdns_recursor"}
: ${CSV:="top-1m.csv"}
: ${IPv6:="1"}
: ${TRACE:="fail"}

if [ $IPv6 = 1 ]
then
	QLA6="::"
else
	QLA6=""
fi

rm -f recursor.pid pdns_recursor.pid
/usr/bin/time --format '%Uuser %Ssystem %Eelapsed %PCPU (%Xtext+%Ddata %Mmax)k
%Iinputs+%Ooutputs (%Fmajor+%Rminor)pagefaults %Wswaps
<measurement><name>user CPU seconds</name><value>%U</value></measurement>
<measurement><name>system CPU seconds</name><value>%S</value></measurement>
<measurement><name>wallclock seconds</name><value>%e</value></measurement>
<measurement><name>%% CPU used</name><value>%P</value></measurement>
'         ${RECURSOR} --daemon=no --local-port=$port --socket-dir=./ --trace=$TRACE --config-dir=. --query-local-address6="${QLA6}" > recursor.log 2>&1 &
sleep 3
../pdns/dnsbulktest -qe 127.0.0.1 $port $limit < ${CSV} > bulktest.results
kill $(cat pdns_recursor.pid)
sleep 5

. ./bulktest.results

mkdir -p recursor-bulktest
rm -f failed_tests passed_tests
touch failed_tests passed_tests
: ${THRESHOLD:="95"}
if [ "$DBT_OKPERCENTAGEINT" -ge $THRESHOLD ]
then
	echo recursor-bulktest >> passed_tests
	RETVAL=0
else
	echo recursor-bulktest >> failed_tests
	RETVAL=1
fi

echo "$DBT_OKPERCENTAGE% of domains resolved" > recursor-bulktest/diff
egrep 'PowerDNS.COM|^<measurement' recursor.log > recursor-bulktest/stdout
echo "<measurement><name>% domains resolved</name><value>$DBT_OKPERCENTAGE</value></measurement>" >> recursor-bulktest/stdout

: ${context:="recursor-bulktest"}

export context
testsdir=. ./toxml

exit $RETVAL
